//
//  SYHttpManager.m
//  songyao
//
//  Created by work on 16/10/19.
//  Copyright © 2016年 刘威 All rights reserved.
//

#import "SYHttpManager.h"
#import "AFHTTPSessionManager.h"
#import "AFNetworkReachabilityManager.h"

@interface SYHttpManager () {
    AFHTTPSessionManager *_manager;
    AFHTTPSessionManager *_managerr;

    AFNetworkReachabilityManager *_reachability;
}

@end

@implementation SYHttpManager
singleton_m(HttpManager)

- (instancetype)init {
    self = [super init];
    if (self) {
        _manager = [AFHTTPSessionManager manager];
        [_manager operationQueue].maxConcurrentOperationCount = 4;
        _reachability = [AFNetworkReachabilityManager managerForDomain:@"https://www.baidu.com/"];
        
//        _manager.requestSerializer = [AFJSONRequestSerializer serializer];
//        _manager.responseSerializer = [AFJSONResponseSerializer serializer];
#warning 超时
        [_manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
        _manager.requestSerializer.timeoutInterval = 12.f;
        [_manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
        
#warning 京东配置
        _manager.responseSerializer.acceptableContentTypes=[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/plain", nil];

        
        _managerr = [AFHTTPSessionManager manager];
        [_managerr operationQueue].maxConcurrentOperationCount = 4;
        
        _managerr.requestSerializer = [AFJSONRequestSerializer serializer];
        _managerr.responseSerializer = [AFJSONResponseSerializer serializer];
#warning 超时
        [_managerr.requestSerializer willChangeValueForKey:@"timeoutInterval"];
        _managerr.requestSerializer.timeoutInterval = 12.f;
        [_managerr.requestSerializer didChangeValueForKey:@"timeoutInterval"];
        
#warning 京东配置
        _managerr.responseSerializer.acceptableContentTypes=[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/plain", nil];
        
#warning 一些设置(忽略一些验证) 测试环境取消注释 正式需要注释掉
//        AFSecurityPolicy *policy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
//        policy.allowInvalidCertificates = YES;
//        policy.validatesDomainName = NO;
//        _manager.securityPolicy = policy;
//        
//        AFSecurityPolicy *policyy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
//        policyy.allowInvalidCertificates = YES;
//        policyy.validatesDomainName = NO;
//        _managerr.securityPolicy = policyy;
#warning --
        
        
        [_reachability setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
            if (status == AFNetworkReachabilityStatusReachableViaWiFi || status == AFNetworkReachabilityStatusReachableViaWWAN) {
                
            } else {
                
            }
        }];
        
        [_reachability startMonitoring];
    }
    return self;
}

#pragma mark - request


- (void)requestGet:(NSString *)url
        parameters:(NSDictionary *)parameters
           success:(void (^)(id responseObject))success
           failure:(void (^)(NSError *error))failure {
    [_manager GET:url parameters:parameters progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        dispatch_async(dispatch_get_main_queue(), ^{
            success(responseObject);
        });
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            failure(error);
        });
    }];
}


- (void)requestPost:(NSString *)url
         parameters:(NSDictionary *)parameters
            success:(void (^)(id responseObject))success
            failure:(void (^)(NSError *error))failure {

//    [_manager.requestSerializer setValue:@"1.0" forHTTPHeaderField:@"v"];
    NSLog(@"%@",parameters);
    [_manager POST:url parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        dispatch_async(dispatch_get_main_queue(), ^{
            success(responseObject);
        });
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            failure(error);
        });
    }];
}


- (void)requestPost:(NSString *)url
     parametersBody:(id)parameters
            success:(void (^)(id responseObject))success
            failure:(void (^)(NSError *error))failure {
    
    [_managerr POST:url parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        dispatch_async(dispatch_get_main_queue(), ^{
            success(responseObject);
        });
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            failure(error);
        });
    }];
}

//- (void)requestRegister:(NSDictionary *)parameters
//                avatars:(UIImage *)avatars
//                success:(void (^)(id responseObject))success
//                failure:(void (^)(NSError *))failure {
//    [_manager.requestSerializer setValue:@"e91fea2cb0d8fcf046a15683766f9792" forHTTPHeaderField:@"DL-Appkey"];
//    
//    [_manager POST:USER_REGISTER_OPT_VALUE parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
//        if (avatars) {
//            [formData appendPartWithFileData:UIImageJPEGRepresentation(avatars,1) name:@"avatar" fileName:@"avatar.png" mimeType:@"image/png"];
//        }
//    } progress:^(NSProgress * _Nonnull uploadProgress) {
//        
//    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        dispatch_async(dispatch_get_main_queue(), ^{
//            success(responseObject);
//        });
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        dispatch_async(dispatch_get_main_queue(), ^{
//            failure(error);
//        });
//    }];
//    
//}

@end
